package top.szhome.framework.starter.swagger;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import top.szhome.framework.starter.swagger.properties.SwaggerProperties;

/**
 * 接口文档配置
 *
 * @author 唐加利
 * @date 2023/7/25
 **/
@Configuration
@EnableKnife4j
@EnableConfigurationProperties(SwaggerProperties.class)
public class SwaggerAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public SwaggerProperties swaggerProperties(){
        return new SwaggerProperties();
    }

    @Bean
    @ConditionalOnMissingBean
    public OpenAPI api(SwaggerProperties swaggerProperties){
        OpenAPI api = new OpenAPI();
        Info info = new Info();
        info.setTitle(swaggerProperties.getTitle());
        info.setDescription(swaggerProperties.getDescription());
        info.setVersion(swaggerProperties.getVersion());
        info.setTermsOfService(swaggerProperties.getTermsOfServiceUrl());
        License license = new License();
        license.setUrl(swaggerProperties.getLicense());
        info.setLicense(license);
        info.setContact(swaggerProperties.getContact());

        api.setInfo(info);
        return api;
    }

}
